// pages/data/data.js
//获取应用实例
const app = getApp()
Page({



  /**
   * 页面的初始数据
   */
  data: {
    article: {},
    articleId: null,
    comment: [],
    sendInfo: '',
    userMessage: '',
    showDialog: false,
    showDialogtwo: false,
    imgs:'',
    names:'',
    noSignIn:'您还未登录,请前往登录',
    commentOne:'我来抢楼',
    fuId: -1,
    nickname:'',
    touserid:-1
  },


  //回到父级评论
  rollBACK(){
    this.setData({
      fuId:-1,
      nickname:null,
      touserid:-1,
      commentOne:'我来抢楼',
    })
  },

  // 点击回复评论
  clickComment(e){
    this.setData({
      fuId:e.currentTarget.dataset.fuid,
      nickname:e.currentTarget.dataset.nickname,
      touserid:e.currentTarget.dataset.touserid,
      commentOne:'回复->'+e.currentTarget.dataset.nickname,
    })
    // console.log(this.data);
  },


  // 文章详细里面的图片放大功能
  bigImage(e){
    wx.previewImage({
      current: 'e.currentTarget.dataset.imgsrc', // 当前显示图片的http链接
      urls: [e.currentTarget.dataset.imgsrc] // 需要预览的图片http链接列表
    })
  },


  /**
   * 发私信
   */
  fasixin(e){
    // console.log(e.currentTarget.dataset.userid);
    let userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '/pages/letter/letter?userid='+userid,
    })
  },

  // 单击头像弹窗
  /**
  * 控制 pop 的打开关闭
  * 该方法作用有2:
  * 1：点击弹窗以外的位置可消失弹窗
  * 2：用到弹出或者关闭弹窗的业务逻辑时都可调用
  */
 toggleDialog() {
  this.setData({
    showDialog: !this.data.showDialog
  });
},
toggleDialogtwo(e) {
  // console.log(e);
  this.setData({
    showDialogtwo: !this.data.showDialogtwo,
    imgs:e.currentTarget.dataset.imgs,
    names:e.currentTarget.dataset.names,
    userid:e.currentTarget.dataset.userid
  });
},

  //----------------------------获取文章详情---------------------------
  article() {
    var that = this;
    wx.request({
      url: app.globalData.myUrl + '/Article/get',
      method: 'GET',
      data: {
        id: that.data.articleId, //文章id
      },
      success: (res) => {
        // console.log(res.data);
        that.setData({
          article: res.data,
        })
      },
      fail: (res) => {},
    })
  },

    //-----------------------------前往登录--------------------------------
    goToLogin(){
      if (!app.globalData.hasLogin) {
        wx.showModal({
          title: '提示',
          content: '是否前往登录',
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后
              wx.switchTab({
                url: '/pages/own/own',
              })
            } else { //这里是点击了取消以后
              that.errShowToast('你还没有登录喔');
            }
          }
        })
        return;
      }
    },
    
  //----------------------------获取评论列表---------------------------
  comment() {
    var that = this;
    wx.request({
      url: app.globalData.myUrl + '/comment/list',
      method: 'GET',
      data: {
        articleId: that.data.articleId, //文章id
      },
      success: (res) => {
        // console.log(res.data);
        that.setData({
          comment: res.data.msg,
        })
      },
      fail: (res) => {},
    })
  },

    //-------------------------删除指定文章----------------------------
    deleteArticle(e) {
      var that = this;
      // console.log(e.currentTarget.id);
      if (!app.globalData.hasLogin) {
        wx.showModal({
					title: '提示',
					content: '你还没有登录喔',
					success: function (res) {
						if (res.confirm) { //这里是点击了确定以后
							wx.switchTab({
								url: '/pages/own/own',
							})
						} else { //这里是点击了取消以后
							that.errShowToast('你还没有登录喔');
						}
					}
				})
        return;
      }
      wx.request({
        url: app.globalData.myUrl + '/Article/delete',
        method:"POST",
        header: {"Content-Type":"application/x-www-form-urlencoded"},
        data: {
          id: that.data.articleId, //文章id
          token: app.globalData.token, //用户令牌
        },
        success: (res) => {
          if (res.data.success) {
            that.sucShowToast(res.data.msg);
            wx.navigateBack({
              delta: 1
            })
          } else {
            that.errShowToast(res.data.msg);
          }
        },
        fail: (res) => {},
      })
    },
  
  //-----------------------------发表评论--------------------------------
  bindMessage: function (e) {
    var msg = e.detail.value.replace(/(\s*$)/g, "");
    this.setData({
      userMessage: msg
    })
  },

  cleanInput: function () {
    // console.log(this.data.userMessage);
    var that = this;
    if (!app.globalData.hasLogin) {
      wx.showModal({
        title: '提示',
        content: '你还没有登录喔',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            wx.switchTab({
              url: '/pages/own/own',
            })
          } else { //这里是点击了取消以后
            that.errShowToast('你还没有登录喔');
            that.setData({
              userMessage:''
            })
          }
        }
      })
      return;
    }
    wx.request({
      url: app.globalData.myUrl + '/comment/deliver',
      method:"POST",
      header: {"Content-Type":"application/x-www-form-urlencoded"},
      data: {
        content:that.data.userMessage,  //评论内容
        articleId: that.data.articleId, //文章id
        commentId: that.data.fuId,
        toUserId:that.data.touserid,
        token:app.globalData.token
      },
      success: (res) => {
        // console.log(res);
        if (res.data.success) {
          that.sucShowToast(res.data.msg)
          that.setData({
            fuId:-1,
            nickname:null,
            touserid:-1,
            commentOne:'我来抢楼',
          })
        }
        that.comment();
      },
      fail: (res) => {},
    })
    this.setData({
      userMessage:''
    })
  },

  //---------------------------评论删除-----------------------------
  deleteComment(e){
    var that = this;
    // console.log(e.currentTarget.id);
    if (!app.globalData.hasLogin) {
      wx.showModal({
        title: '提示',
        content: '你还没有登录喔',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            wx.switchTab({
              url: '/pages/own/own',
            })
          } else { //这里是点击了取消以后
            that.errShowToast('你还没有登录喔');
          }
        }
      })
      return;
    }
    wx.request({
      url: app.globalData.myUrl+'/comment/delete',
      method:"POST",
      header: {"Content-Type":"application/x-www-form-urlencoded"},
      data: {
        id: e.currentTarget.id,
        token: app.globalData.token
      },
      success: (res) => {
        // console.log(res);
        if (res.data.success) {
          that.sucShowToast(res.data.msg);
          that.comment();
        }else{
          that.errShowToast(res.data.msg);
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },

  //---------------------------函数封装------------------------------
  errShowToast: function (toastText) {
    wx.showToast({
      title: toastText,
      image: '/images/password.png',
      duration: 2000
    });
  },
  sucShowToast: function (toastText) {
    wx.showToast({
      title: toastText,
      duration: 2000
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      articleId: options.id,
      hasLogin:app.globalData.hasLogin,
      // articleId: 33
    });
    this.article();
    this.comment();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      articleId: options.id,
      hasLogin:app.globalData.hasLogin,
      // articleId: 33
    });
    this.article();
    this.comment();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 分享到朋友圈
   */
  // onShareTimeline: function() {
  //   return {
  //     title: '一个小demo',
  //     imageUrl: "https://www.kongbai121.xyz/drawing-bed/2023-01/59e40761-c197-4b17-be36-bfbd1f2cfa1c.jpg"
  //   }
  // },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '一个小demo',
      path: '/pages/loading/loading',
      imageUrl: "https://www.kongbai121.xyz/drawing-bed/2023-01/59e40761-c197-4b17-be36-bfbd1f2cfa1c.jpg"
    }
  }
})